unit pcneDataSet;

interface uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, ComCtrls, ExtCtrls, Grids, StdCtrls,
  pcneAuxiliar;

//
procedure gravarDataSet(grid: TstringGrid; chave: string);
procedure gravarDataSet1(grid: TstringGrid; chave, DataSet: string);
procedure gravarDataSet2(grid: TstringGrid; chave, DataSet: string);
procedure carregarDataSet(grid: TstringGrid; chave: string);
procedure carregarDataSet1(grid: TstringGrid; chave, DataSet: string);
procedure carregarDataSet2(grid: TstringGrid; chave, DataSet: string);
procedure LimparDataSet1(grid: TstringGrid);
function AtualizarKey(grid: TstringGrid; CodigoKey: string): integer;
//
procedure CarregarInformacoes(Grid: TstringGrid);
procedure CarregarInformacoes0;
procedure CarregarInformacoes1(Grid: TstringGrid);
procedure CarregarInformacoes2(Grid: TstringGrid);
procedure CarregarInformacoesG2M(Grid: TstringGrid; Memo: TMemo);
//
procedure GravarInformacoesGrid(grid: TStringGrid);
procedure GravarInformacoesGrid10(grid: TStringGrid);
procedure GravarInformacoesGrid11(grid: TStringGrid);
procedure GravarInformacoesGrid20(grid: TStringGrid);

var
  puNota: TstringList;
  puDataSet: TStringList;
  pArquivo: TstringList;

implementation

procedure gravarDataSet(grid: TstringGrid; chave: string);
begin
  chave := Trim(chave);
  if gTipo(grid, 1) then
    gravarDataSet1(grid, chave, gNomeDS(grid));
  if gTipo(grid, 2) then
    gravarDataSet2(grid, chave, gNomeDS(grid));
end;

procedure gravarDataSet1(grid: TstringGrid; chave, dataSet: string);
var
  iRow, j, k: integer;
  s: string;
begin
  for iRow := 0 to grid.RowCount - 1 do
    for j := 0 to puDataSet.Count - 1 do
      if pos(dataSet + '(' + chave + ')' + TUG(grid, tsXYId, iRow) + '|', puDataSet[j]) = 1 then
        puDataSet[j] := '';
  for iRow := 1 to grid.RowCount - 1 do
  begin
    k := -1;
    for j := 0 to puDataSet.Count - 1 do
      if (k = -1) and (trim(puDataSet[j]) = '') then
        k := j;
    s := Trim(grid.cells[tsXYConteudo, iRow]);
    if s <> '' then
    begin
      if grid.cells[tsXYSelecionavel, iRow] = 'S' then
        s := IDSel(s);
      s := dataSet + '(' + chave + ')' + TUG(grid, tsXYId, iRow) + '|' + s;
      if pos('+|', s) = 0 then
      begin
        if k = -1 then
          puDataSet.add(s);
        if k <> -1 then
          puDataSet.Strings[k] := s;
      end;
    end;
  end;
end;

procedure gravarDataSet2(grid: TstringGrid; chave, DataSet: string);
var
  iCol, iRow, j, k: integer;
  s: string;
begin
  for iCol := 1 to grid.ColCount - 1 do
    for j := 0 to puDataSet.Count - 1 do
      if (pos(dataSet + '(' + chave + ')', puDataSet[j]) = 1) and (pos(TUG(grid, icol, tsXYId) + '|', puDataSet[j]) > 0) then
        puDataSet[j] := '';
  for iRow := grid.FixedRows to grid.RowCount - 1 do
    for iCol := 1 to grid.ColCount - 1 do
    begin
      k := -1;
      for j := 0 to puDataSet.Count - 1 do
        if (k = -1) and (trim(puDataSet[j]) = '') then
          k := j;
      s := trim(grid.cells[iCol, iRow]);
      if s <> '' then
      begin
        if grid.cells[iCol, tsXYSelecionavel] = 'S' then
          s := IDSel(s);
        s := dataSet + '(' + chave + ')LINHA<' + IntToStr(iRow - grid.FixedRows + 1) + '>' + TUG(grid, iCol, tsXYId) + '|' + s;
        if pos('+|', s) = 0 then
        begin
          if k = -1 then
            puDataSet.add(s);
          if k <> -1 then
            puDataSet.Strings[k] := s;
        end;
      end;
    end;
end;

procedure carregarDataSet(grid: TstringGrid; chave: string);
begin
  chave := Trim(chave);
  if gTipo(grid, 1) then
    CarregarDataSet1(grid, chave, gNomeDS(grid));
  if gTipo(grid, 2) then
    CarregarDataSet2(grid, chave, gNomeDS(grid));
end;

procedure carregarDataSet1(grid: TstringGrid; chave, dataSet: string);
var
  iRow, j, k: integer;
  s: string;
begin
  for iRow := grid.FixedRows to grid.RowCount - 1 do
  begin
    grid.Cells[tsXYConteudo, iRow] := '';
    if grid.Cells[tsXYSelecionavel, iRow] = 'S' then
      grid.Cells[tsXYConteudo, iRow] := CarregarTipoDefault(grid.Cells[tsXYId, iRow])
  end;

  if puDataSet.Count - 1 <= 0 then
    exit;

  for iRow := 0 to grid.RowCount - 1 do
  begin
    j := -1;
    while j < puDataSet.Count - 1 do
    begin
      inc(j);
      if pos(dataSet + '(' + chave + ')' + TUG(grid, tsXYId, iRow) + '|', puDataSet[j]) = 1 then
      begin
        s := copy(puDataSet[j], pos('|', puDataSet[j]) + 1, maxInt);
        if (grid.cells[tsXYSelecionavel, iRow] = 'S') and (s <> '') then
          grid.cells[tsXYConteudo, iRow] := CarregarTipo(TUG(grid, tsXYId, iRow), s)
        else
          grid.cells[tsXYConteudo, iRow] := s;
        j := 999999999;
      end;
    end;
  end;
end;

procedure carregarDataSet2(grid: TstringGrid; chave, dataSet: string);
var
  iCol, iRow, j, k, m: integer;
  s: string;
begin
  grid.RowCount := grid.FixedRows + 1;
  iRow := grid.FixedRows;
  for iCol := 0 to grid.ColCount - 1 do
  begin
    if grid.Cells[iCol, tsXYSelecionavel] = 'S' then
      grid.Cells[iCol, iRow] := CarregarTipoDefault(grid.Cells[iCol, tsXYId])
    else
      grid.Cells[iCol, iRow] := '';
  end;
  if puDataSet.Count - 1 <= 0 then
    exit;
  iRow := grid.FixedRows - 1;
  m := 0;
  while m = 0 do
  begin
    inc(iRow);
    m := 1;
    for iCol := 0 to grid.ColCount - 1 do
    begin
      j := -1;
      while j < puDataSet.Count - 1 do
      begin
        inc(j);
        if pos(dataSet + '(' + chave + ')LINHA<' + IntToStr(iRow - grid.FixedRows + 1) + '>' + TUG(grid, iCol, tsXYId) + '|', puDataSet[j]) = 1 then
        begin
          m := 0;
          if iRow > grid.RowCount - 1 then
          begin
            grid.RowCount := grid.RowCount + 1;
            grid.RowHeights[grid.RowCount - 1] := pu2Conteudo;
          end;
          s := trim(copy(puDataSet[j], pos('|', puDataSet[j]) + 1, maxInt));
          if ((grid.cells[iCol, tsXYSelecionavel] = 'S') and (s <> '')) then
            grid.cells[iCol, iRow] := CarregarTipo(TUG(grid, iCol, tsXYId), s)
          else
            grid.cells[iCol, iRow] := s;
          j := 999999999;
        end;
      end;
    end;
  end;
end;

procedure LimparDataSet1(grid: TstringGrid);
var
  iRow, i: integer;
  KY1s: string;
begin
  KY1s := '';
  for iRow := grid.FixedRows - 1 to grid.rowCount - 1 do
    KY1s := KY1s + '*PRODUTO(' + grid.Cells[gCol('K=1', grid), iRow] + ')LINHA<*';
  for i := 0 to puDataSet.count - 1 do
    if pos(copy(puDataSet[i], 1, pos('<', puDataSet[i])), KY1s) = 0 then
      puDataSet[i] := '';
  i := -1;
  while i < puDataSet.Count - 1 do
  begin
    inc(i);
    if puDataSet[i] = '' then
    begin
      puDataSet.Delete(i);
      i := 0;
    end;
  end;
end;

function AtualizarKey(grid: TstringGrid; CodigoKey: string): integer;
var
  iRow, iMax: integer;
  s: string;
begin
  iMax := 0;
  if (gSame(grid, 'G20ITENS')) or (gSame(grid, 'G20VOLUMES')) then
  begin
    for iRow := grid.FixedRows to grid.RowCount - 1 do
    begin
      s := grid.Cells[gCol(CodigoKey, grid), iRow];
      if s <> '' then
        if StrToInt(s) > iMax then
          inc(iMax);
    end;
    Result := iMax;
    exit;
  end;
  iMax := 0;
  for iRow := 0 to puNota.Count - 1 do
  begin
    if pos('>' + CodigoKey + '|', puNota[iRow]) > 0 then
    begin
      s := copy(puNota[iRow], pos('|', puNota[iRow]) + 1, maxInt);
      if StrToInt(s) > iMax then
        inc(iMax);
    end;
  end;
  Result := iMax;
end;

////////////////////////////////////////////////////////////////////////////////

procedure CarregarInformacoes(Grid: TstringGrid);
begin
  if gTipo(grid, 1) then
    CarregarInformacoes1(Grid);
  if gTipo(grid, 2) then
    CarregarInformacoes2(Grid);
  if TU(grid.name) = 'G20ITENS' then
    CarregarInformacoes0;
end;

procedure CarregarInformacoes0;
var
  i, j: integer;
begin
  //puDataSet.clear;
  i := 0;
  for j := 0 to puNota.Count - 1 do
  begin
    if (pos('DATASET|BEGIN', puNota[j]) = 1) and (i = 0) then
      i := 1;
    //if (pos('DATASET|BEGIN', puNota[j]) = 0) and (pos('DATASET|END', puNota[j]) = 0) and (i = 1) then
    //  puDataSet.add(puNota[j]);
    if (pos('DATASET|END', puNota[j]) = 1) and (i = 1) then
      i := 2;
  end;
end;

procedure CarregarInformacoes1(Grid: TstringGrid);
var
  i, j, k: integer;
  s: string;
begin
  for i := 0 to grid.RowCount - 1 do
  begin
    for j := 0 to puNota.Count - 1 do
    begin
      if (pos(TUG(grid, tsXYid, i) + '|', puNota[j]) = 1) then
      begin
        // Conteudo
        s := Trim(copy(puNota[j], pos('|', puNota[j]) + 1, maxInt));
        grid.Cells[tsXYdefault, i] := s;
        grid.Cells[tsXYconteudo, i] := s;
        // Selecionavel
        if grid.cells[tsXYSelecionavel, i] = 'S' then
        begin
          for k := 0 to puTipo.Count - 1 do
          begin
            if CCpS(ttCampoID, puTipo[k]) = TUG(grid, tsXYID, i) then
            begin
              s := CCpS(ttConteudo, puTipo[k]);
              if pos(grid.Cells[tsXYconteudo, i] + ' - ', s) > 0 then
              begin
                grid.Cells[tsXYdefault, i] := s;
                grid.Cells[tsXYconteudo, i] := s;
              end;
            end;
          end;
        end;
        //
      end;
    end;
  end;
end;

procedure CarregarInformacoes2(Grid: TstringGrid);
var
  i, iRow, iCol, iMax, iParametro, iLinha: integer;
  s: string;
begin
  iMax := 0;
  for iCol := 0 to grid.colcount - 1 do
  begin
    for iLinha := 0 to puNota.Count - 1 do
    begin
      if pos('LINHA<', puNota[iLinha]) = 1 then
      begin
        s := TUG(grid, iCol, tsXYid);
        if pos('>' + s + '|', puNota[iLinha]) > 0 then
        begin
          i := StrToInt(copy(puNota[iLinha], pos('<', puNota[iLinha]) + 1, pos('>', puNota[iLinha]) - pos('<', puNota[iLinha]) - 1));
          if i > iMax then
            iMax := i;
        end;
      end;
    end;
  end;
  Grid.RowCount := Grid.FixedRows + 1;
  if iMax = 0 then
    exit;
  s := Grid.Name;
  Grid.RowCount := Grid.FixedRows + iMax;
  for iRow := 1 to iMax do
  begin
    for iCol := 0 to grid.ColCount - 1 do
    begin
      for iParametro := 0 to puNota.Count - 1 do
      begin
        if pos('LINHA<', puNota[iParametro]) = 1 then
        begin
          if pos('<' + IntToStr(iRow) + '>' + TUG(grid, iCol, tsXYid) + '|', puNota[iParametro]) > 0 then
          begin
            // Conteudo
            s := Trim(copy(puNota[iParametro], pos('|', puNota[iParametro]) + 1, maxInt));
            grid.RowHeights[Grid.FixedRows - 1 + iRow] := pu2Conteudo;
            grid.Cells[iCol, Grid.FixedRows - 1 + iRow] := s;
            grid.Cells[tsXYdefault, Grid.FixedRows - 1 + iRow] := s;
          end;
          //
        end;
      end;
    end;
  end;
end;

procedure CarregarInformacoesG2M(Grid: TstringGrid; Memo: TMemo);
var
  i: integer;
begin
  Memo.ReadOnly := puLockAll;
  Memo.Lines.Clear;
  for i := Grid.FixedRows to Grid.RowCount - 1 do
    Memo.Lines.add(Grid.Cells[1, i]);
  if (Memo.Lines.Count = 1) and (trim(Memo.Lines.Strings[0]) = '') then
    Memo.Lines.Clear;
end;

////////////////////////////////////////////////////////////////////////////////

procedure GravarInformacoesGrid(grid: TStringGrid);
begin
  if (gTipo(grid, 1)) and (gNumeroDS(grid, 0)) then
    GravarInformacoesGrid10(grid);
  if (gTipo(grid, 1)) and (gNumeroDS(grid, 1)) then
    GravarInformacoesGrid11(grid);
  if (gTipo(grid, 2)) and (gNumeroDS(grid, 0)) then
    GravarInformacoesGrid20(grid);
end;

procedure GravarInformacoesGrid10(grid: TStringGrid);
var
  iRow: integer;
  s: string;
begin
  for iRow := grid.fixedrows to grid.rowcount - 1 do
  begin
    s := trim(grid.Cells[TSXYCONTEUDO, iRow]);
    if grid.Cells[TSXYSELECIONAVEL, iRow] = 'S' then
      s := IDSel(s);
    if s <> '' then
      pArquivo.Add(TUG(grid, tsXYID, iRow) + '|' + s);
  end;
end;

procedure GravarInformacoesGrid11(grid: TStringGrid);
var
  iRow: integer;
  s: string;
begin
  for iRow := grid.FixedRows to grid.rowcount - 1 do
  begin
    s := trim(grid.Cells[TSXYCONTEUDO, iRow]);
    if grid.Cells[TSXYSELECIONAVEL, iRow] = 'S' then
      s := IDSel(s);
    if s <> '' then
      pArquivo.Add(TUG(grid, tsXYID, iRow) + '|' + s);
  end;
end;

procedure GravarInformacoesGrid20(grid: TStringGrid);
var
  iRow, iCol: integer;
  s: string;
begin
  for iRow := grid.FixedRows to grid.rowCount do
    for iCol := grid.FixedCols - 1 to grid.colCount - 1 do
    begin
      s := Trim(grid.Cells[iCol, iRow]);
      if s <> '' then
      begin
        if grid.Cells[iCol, TSXYSELECIONAVEL] = 'S' then
          s := IDSel(s);
        if TUG(grid, iCol, tsXYID) <> 'ID' then
          pArquivo.Add('LINHA<' + IntToStr(iRow - grid.fixedrows + 1) + '>' + TUG(grid, iCol, tsXYID) + '|' + s);
      end;
    end;
end;

end.

